package com.myjoje.controller.admin;

import com.alibaba.excel.EasyExcel;
import com.myjoje.service.admin.AdminUserService;
import com.myjoje.util.EasyExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/**
 * 后台用户管理，提供用户导入
 */
@Controller
@RequestMapping("/admin/user")
public class AdminUserController {

    @Autowired
    private AdminUserService adminUserService;

    /**
     * 用户管理平台
     */
    @RequestMapping("/admin_user_index")
    public String adminUserIndex() {
        return "/admin/admin_user_index";
    }


    /**
     * 下载用户导入模板
     */
    @RequestMapping("/downloadUserTemplate")
    public void downloadUserTemplate(HttpServletResponse response) throws Exception {
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("用户导入模板", "utf-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        String headers = "姓名(昵称),用户名,手机号码,性别";
        List<List<String>> excelHeader = EasyExcelUtil.getExcelHeader(headers);
        EasyExcel.write(response.getOutputStream())
                .sheet("用户导入模板").head(excelHeader).doWrite(new ArrayList());
    }
}
